package com.woniuxy.microretailserver.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microretailserver.bean.GoodsOrderExportBean;
import com.woniuxy.microretailserver.bean.GoodsReturnInfoBean;
import com.woniuxy.microretailserver.bean.PageRequest;
import com.woniuxy.microretailserver.service.IGoodsReturnInfoService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenhu
 * 终端退款控制层
 */
@RequestMapping("/return")
@RestController
public class ReturnGoodsOrderController {

    @Resource
    private IGoodsReturnInfoService goodsReturnInfoServiceImpl;

    /**
     * 导出退货订单数据
     * @throws Exception
     */
    @GetMapping(value = "/order",produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveOrder() throws Exception {
        List<GoodsReturnInfoBean> all = goodsReturnInfoServiceImpl.findAll();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("退货订单表", "退货订单数据"), GoodsReturnInfoBean.class, all);
        FileOutputStream outputStream = new FileOutputStream("C:/Users/Administrator/Desktop/goodsReturnOrder.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    /**
     * 展示终端退款数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/findreturn",produces = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<GoodsReturnInfoBean> findReturnOrder(PageRequest request){
        GoodsReturnInfoBean goodsReturnInfoBean = new GoodsReturnInfoBean();
        PageInfo<GoodsReturnInfoBean> allGoodsReturn = goodsReturnInfoServiceImpl.findAllGoodsReturn(request, goodsReturnInfoBean);
        return allGoodsReturn;
    }

    /**
     * 模糊查询退款数据
     * @param request
     * @param goodsReturnInfoBean
     * @return
     */
    @GetMapping(value = "/module",produces = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<GoodsReturnInfoBean> findReturnGoodsOrder(PageRequest request,GoodsReturnInfoBean goodsReturnInfoBean){
        PageInfo<GoodsReturnInfoBean> allGoodsReturn = goodsReturnInfoServiceImpl.findAllGoodsReturn(request, goodsReturnInfoBean);
        return allGoodsReturn;
    }

    /**
     * 根据退款订单号查询全部数据，终端退货单详情
     * @param orderNo
     * @return
     */
    @GetMapping(value = "findNo",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String, Object> findAllReturn(String orderNo){
        HashMap<String, Object> allReturn = goodsReturnInfoServiceImpl.findAllReturn(orderNo);
        System.out.println(allReturn);
        return allReturn;
    }
}
